﻿using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ZH.GPT.CustomMiddleWare
{
    /// <summary>
    /// 记录操作日志的MiddleWare
    /// </summary>
    public class OperationLogMiddleWare
    {
        /// <summary>
        /// 下一个中间件
        /// </summary>
        private readonly RequestDelegate _next;

        ILogger<OperationLogMiddleWare> logger;

        /// <summary>
        /// 记录操作日志的MiddleWare
        /// </summary>
        /// <param name="next">下一个中间件</param>
        /// <param name="logger">日志</param>
        public OperationLogMiddleWare(RequestDelegate next, ILogger<OperationLogMiddleWare> logger)
        {
            _next = next;
            this.logger = logger;
        }


        /// <summary>
        /// 调用
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task Invoke(HttpContext context)
        {
            ////此处处理操作日志
            //if (context.Request.Path.Value.Contains("GetUser"))
            //{
            //    this.logger.LogError($"查询用户信息\r\n");
            //}

            await _next(context);
            //此处可以从Response中拿到返回信息再处理


        }
    }
}
